package leetcode.code1367;

import leetcode.helper.tree.ListNode;
import leetcode.helper.tree.TreeNode;

public class Solution {
	public boolean isSubPath(ListNode head, TreeNode root) {
		if (root == null) {
			return false;
		}
		if (this.f(head, root, head)) {
			return true;
		}
		if (this.isSubPath(head, root.left) || this.isSubPath(head, root.right)) {
			return true;
		}
		return false;
	}

	private boolean f(ListNode head, TreeNode root, ListNode cur) {
		if (cur == null) {
			return true;
		}
		if (root == null) {
			return false;
		}
		if (root.val == cur.val && (this.f(head, root.left, cur.next) || this.f(head, root.right, cur.next))) {
			return true;
		}
		return false;
	}
}
